#include <stm32f4xx.h>
#include "ov7670.h"
#include "dcmi_hal.h"
#include "i2c_hal.h"

//#include "stm32f429i_discovery.h"

#define SLAVE_ADDRESS 0x21

static int ov7670_write_array(struct regval_list *vals)
{
    while (vals->reg_num != 0xff || vals->value != 0xff) {
        i2c_write(SLAVE_ADDRESS, vals->reg_num, 1, &(vals->value));
        vals++;
    }
    return 0;
}

int ov7670_init(void)
{
    i2c_open();
    dcmi_open();
    return ov7670_write_array(ov7670_default_regs);
}

int ov7670_detect(void)
{
    unsigned char data;
    int ret;

    ret = ov7670_init();
    if (ret < 0)
        return ret;
    i2c_read(SLAVE_ADDRESS, REG_MIDH, &data);
    if (data != 0x7f) /* OV manuf. id. */
        return 1;
    i2c_read(SLAVE_ADDRESS, REG_MIDL, &data);
    if (data != 0xa2)
        return 1;
    /*
     * OK, we know we have an OmniVision chip...but which one?
     */
    i2c_read(SLAVE_ADDRESS, REG_PID, &data);
    if (data != 0x76)  /* PID + VER = 0x76 / 0x73 */
        return 1;
#if 0
    i2c_read(SLAVE_ADDRESS, REG_VER, &data);
    if (ret != 0x73)  /* PID + VER = 0x76 / 0x73 */
        return 1;
#endif
    return 0;
}